Logical AND for Regex

 

How to write a Regular Expression that does an ''AND'' for the occurrence of two patterns? Answer: use a non-consuming "''?=''" operator! From [1]:

The typical (i.e. Perl/Java) notation is:
     (?=expr)
This means "match expr but after that continue matching at the original match-point."

You can do as many of these as you want, and this will be an "and." Example:
     (?=match this expression)(?=match this too)(?=oh, and this)
You can even add capture groups inside the non-consuming expressions if you need to save some of the data therein.

... and from [2] to try it out:

perl -e "q{some stuff and things} =~ /(?=.*some)(?=.*stuff)(?=.*things)/ ? print 'yes' : print 'no'"

... clever and useful!

(cf Snip Pattern (2001-09-06), Reg Explanations (2003-12-03), Regex vs HTML (2020-02-27), ...) - ^z - 2020-10-28